#include "window_common.h"
#include "gkt_door.h"

#if !GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_FIXED

enum {
	WIDGET_ID_TITLE = 0,
	WIDGET_ID_LIST,
	WIDGET_ID_HINT,
	WIDGET_ID_OK_CANCEL,
	WIDGET_ID_MAX
};

enum {
	TIMER_ID_TIMEOUT = 0,
	TIMER_ID_MAX
};

static const gkt_widget_list_item_s sc_window_lock_open_direction_widget_list_items[] = {
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_TEXT_ID_left_open, 
			GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_FORWARD),
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_TEXT_ID_right_open, 
			GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_REVERSE),
};
static const gkt_widget_list_attr_s sc_window_lock_open_direction_widget_list_attr = 
	GKT_WIDGET_LIST_STATIC_ATTR(WINDOW_WIDGET_NORMAL_HEIGHT,
		sizeof(sc_window_lock_open_direction_widget_list_items) / sizeof(gkt_widget_list_item_s),
		sc_window_lock_open_direction_widget_list_items);

GKT_WINDOW_WIDGET_DESCS_DEFINE(lock_open_direction) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC( 
		GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_TITLE_MARK,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
		0, 0, 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_lock_open_direction
	),
	[WIDGET_ID_LIST] = GKT_WIDGET_LIST_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT, 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT * (WINDOW_NORMAL_ROWS - 2),
		&sc_window_lock_open_direction_widget_list_attr
	),
	[WIDGET_ID_HINT] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, (WINDOW_WIDGET_NORMAL_HEIGHT / 2) * 3, 
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		NULL),
	[WIDGET_ID_OK_CANCEL] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT * (WINDOW_NORMAL_ROWS - 1), 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_cancel_ok
	),
};

enum {
	WINDOW_ST_INIT = 0,
	WINDOW_ST_CFG_UPDATING,
	WINDOW_ST_LOCK_OPENING,
	WINDOW_ST_LOCK_OPEN_CONFIRM,
	WINDOW_ST_LOCK_OPENED,
	WINDOW_ST_MAX
};

typedef struct _window_motor_rotate_direction {
	int	state;
	gkt_door_lock_motor_rotate_direction_e	direction;
} window_motor_rotate_direction_s;
static window_motor_rotate_direction_s s_window_motor_rotate_direction;

static void window_lock_open_direction_lock_opened(void)
{
#if defined(GKT_CONFIG_DOOR_LOCK_ENABLE) && GKT_CONFIG_DOOR_LOCK_ENABLE	
	s_window_motor_rotate_direction.state = WINDOW_ST_LOCK_OPENED;

	gp_window_unreset_data->lock_motor_rotate_direction = 
		s_window_motor_rotate_direction.direction;
	gp_window_unreset_data->lock_motor_rotate_direction_set = 1;
	window_data_save();

	window_dialog_show_big_prompt(WINDOW_DIALOG_HINT_SUCCESS,
			WINDOW_TEXT_ID_lock_open_direction,
			WINDOW_TEXT_ID_set_success,
			WINDOW_VOICE_COMBINED_ID(set_success));
#endif	
	gkt_window_exit();
}
static void window_lock_open_direction_failed(void)
{
	s_window_motor_rotate_direction.state = WINDOW_ST_MAX;
	window_dialog_show_big_prompt(WINDOW_DIALOG_HINT_FAILED,
			WINDOW_TEXT_ID_lock_open_direction,
			WINDOW_TEXT_ID_set_fail,
			WINDOW_VOICE_COMBINED_ID(set_failed));
}

#if defined(GKT_CONFIG_WINDOW_LOCK_OPEN_DIRECTION_CONFIRM_ENABLE) \
	&& GKT_CONFIG_WINDOW_LOCK_OPEN_DIRECTION_CONFIRM_ENABLE
static void window_lock_open_direction_lock_open_confirm(void)
{
	s_window_motor_rotate_direction.state = WINDOW_ST_LOCK_OPEN_CONFIRM;
	window_dialog_show_big_confirm(WINDOW_DIALOG_HINT_IMPORTANT,
			WINDOW_TEXT_ID_lock_open_direction,
			WINDOW_TEXT_ID_lock_is_opened,
			WINDOW_VOICE_COMBINED_ID(lock_already_opened));
}
static void window_lock_open_direction_open_lock(void)
{
	uint32_t timeout;
	int retval;

	s_window_motor_rotate_direction.state = WINDOW_ST_LOCK_OPENING;
	gkt_widget_set_data_by_id(WIDGET_ID_HINT, WINDOW_TEXT_ID_lock_opening);

	gkt_door_lock_reset();

	retval = gkt_door_lock_open();
	if (GKT_SUCCESS == retval)
		window_lock_open_direction_lock_open_confirm();
	else if (GKT_EINPROCESS == retval) {
		timeout = gkt_door_lock_get_timeout(GKT_DOOR_LOCK_OP_OPEN) + GKT_DOOR_LOCK_WINDOW_TIME_OUT_APPEND_MS;
		gkt_window_timer_set(TIMER_ID_TIMEOUT, timeout, 0, 0);
	}
	else
		window_lock_open_direction_failed();
}
#else
#define window_lock_open_direction_open_lock()	\
	window_lock_open_direction_lock_opened()
#endif

static void window_lock_open_direction_cfg_update(void)
{
	int retval;

	s_window_motor_rotate_direction.state = WINDOW_ST_CFG_UPDATING;
	window_common_mask_all();

#if (defined(GKT_CONFIG_WINDOW_LOCK_OPEN_DIRECTION_CONFIRM_ENABLE) \
		&& GKT_CONFIG_WINDOW_LOCK_OPEN_DIRECTION_CONFIRM_ENABLE) \
	|| (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
	gkt_widget_hide_by_id(WIDGET_ID_LIST);
	gkt_widget_set_data_by_id(WIDGET_ID_HINT, WINDOW_TEXT_ID_update_setting);
	gkt_widget_set_showable_by_id(WIDGET_ID_HINT, 1);
	gkt_widget_show_by_id(WIDGET_ID_HINT);
	gkt_widget_hide_by_id(WIDGET_ID_OK_CANCEL);
#endif

	retval = gkt_door_lock_set_motor_rotate_direction(s_window_motor_rotate_direction.direction);
	if (GKT_SUCCESS == retval)
		window_lock_open_direction_open_lock();
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
	else if (GKT_EINPROCESS == retval)
		gkt_window_timer_set(TIMER_ID_TIMEOUT, 2000, 0, 0);
#endif
	else
		window_lock_open_direction_failed();
}

static int window_lock_open_direction_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_CONTINUE;

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_INIT == minor_type) {
				s_window_motor_rotate_direction.state = WINDOW_ST_INIT;
#if defined(GKT_CONFIG_DOOR_LOCK_ENABLE) && GKT_CONFIG_DOOR_LOCK_ENABLE					
				s_window_motor_rotate_direction.direction = gp_window_unreset_data->lock_motor_rotate_direction;
#endif				
				if (s_window_motor_rotate_direction.direction >= GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_MAX)
					s_window_motor_rotate_direction.direction = GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_FORWARD;
			}
			else if (GKT_WINDOW_ACTIVE == minor_type) {
#if defined(GKT_CONFIG_WINDOW_LOCK_OPEN_DIRECTION_CONFIRM_ENABLE) \
	&& GKT_CONFIG_WINDOW_LOCK_OPEN_DIRECTION_CONFIRM_ENABLE
				if (WINDOW_ST_LOCK_OPEN_CONFIRM == s_window_motor_rotate_direction.state) {
					int decision = window_dialog_decision();
					if (WINDOW_DIALOG_DECISION_OK == decision) {
						window_lock_open_direction_lock_opened();
						return GKT_EVENT_HANDLE_COMPLETE;
					}
				}
#endif
				window_common_mask_except_updn_cancel_ok();
				gkt_widget_set_showable_by_id(WIDGET_ID_HINT, 0);
				if (data) {
					gkt_widget_list_set_focus_by_data(WIDGET_ID_LIST, 
							s_window_motor_rotate_direction.direction);
				}
			}
			break;
		case GKT_EVENT_WIDGET:
			if (GKT_WIDGET_EVENT_CLICK == minor_type) {
				s_window_motor_rotate_direction.direction = sc_window_lock_open_direction_widget_list_items[data].data;
				window_lock_open_direction_cfg_update();
				retval = GKT_EVENT_HANDLE_COMPLETE;
			}
			break;
		case GKT_EVENT_TIMER:
			if ((WINDOW_ST_CFG_UPDATING == s_window_motor_rotate_direction.state)
				|| (WINDOW_ST_LOCK_OPENING == s_window_motor_rotate_direction.state))
			{
				window_lock_open_direction_failed();
			}
			break;
		case GKT_EVENT_SYSTEM:
			if (GKT_WINDOW_SYSTEM_DOOR == minor_type) {
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
				if ((GKT_DOOR_NOTIFY_LOCK_CONFIG == id)
					&& (WINDOW_ST_CFG_UPDATING == s_window_motor_rotate_direction.state)) 
				{
					gkt_window_timer_clear(TIMER_ID_TIMEOUT);
					if (GKT_SUCCESS == data)
						window_lock_open_direction_open_lock();
					else
						window_lock_open_direction_failed();

					return GKT_EVENT_HANDLE_COMPLETE;
				}
#endif
#if defined(GKT_CONFIG_WINDOW_LOCK_OPEN_DIRECTION_CONFIRM_ENABLE) \
	&& GKT_CONFIG_WINDOW_LOCK_OPEN_DIRECTION_CONFIRM_ENABLE
				if ((GKT_DOOR_NOTIFY_LOCK_STATUS == id)
					&& (WINDOW_ST_LOCK_OPENING == s_window_motor_rotate_direction.state))
				{
					gkt_window_timer_clear(TIMER_ID_TIMEOUT);
					if (GKT_DOOR_LOCK_ST_OPENED == data)
						window_lock_open_direction_lock_open_confirm();
					else if (GKT_DOOR_LOCK_ST_OPEN_FAILED == data)
						window_lock_open_direction_failed();

					return GKT_EVENT_HANDLE_COMPLETE;
				}
#endif
			}
			break;
		default:
			break;
	}

	return retval;
}
GKT_WINDOW_DESC_DEFINE(lock_open_direction);

#endif

